package com.du.dumall.product.service.impl;



import com.du.dumall.product.dao.AttrAttrgroupRelationDao;
import com.du.dumall.product.dao.AttrDao;
import com.du.dumall.product.entity.AttrAttrgroupRelationEntity;
import com.du.dumall.product.entity.AttrEntity;
import com.du.dumall.product.entity.CategoryEntity;
import com.du.dumall.product.service.AttrService;
import com.du.dumall.product.service.CategoryService;
import com.du.dumall.product.vo.AttrGroupRelationVo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.du.dumall.common.utils.PageUtils;
import com.du.dumall.common.utils.Query;

import com.du.dumall.product.dao.AttrGroupDao;
import com.du.dumall.product.entity.AttrGroupEntity;
import com.du.dumall.product.service.AttrGroupService;




@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    CategoryService categoryService;


    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (catId != null && catId > 0){
            queryWrapper.eq("catelog_id",catId);
        }

        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                    wrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
                }
            );
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Long[] getCatelogPath(Long catelogId) {
        ArrayList<Long> ids = new ArrayList<>();
        findParentCid(catelogId,ids);
        Collections.reverse(ids);

        return (Long[]) ids.toArray(new Long[ids.size()]);
    }



    private void findParentCid(Long catelogId, ArrayList<Long> ids ){
        ids.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if(categoryEntity.getParentCid() != null &&categoryEntity.getParentCid()>0){
            findParentCid(categoryEntity.getParentCid(),ids);
        }
    }

    private CategoryEntity getById(Long catelogId){
        return  categoryService.getById(catelogId);
    }


    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
        List<AttrAttrgroupRelationEntity> relationEntities = Arrays.asList(vos).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationDao.deleteRelation(relationEntities);
    }


}